package online.hxstart.code.concurrent.chapter03;

import static java.util.stream.Collectors.toList;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author huxing
 * @date 19-4-23 星期二 下午7:23
 * @describe
 */
public class FightQueryExample {
  private static List<String> fightCompany = Arrays.asList("CSA", "CEA", "HNA", "SSS");

  public static void main(String[] args) {
    List<String> result = search("SH", "BJ");
    System.out.println("===========result=========");
    result.forEach(System.out::println);
  }

  private static List<String> search(String original, String dest) {
    final List<String> result = new ArrayList<>();
    List<FightQueryTask> tasks =
        fightCompany.stream().map(c -> createSearchTask(c, original, dest)).collect(toList());
    tasks.forEach(Thread::start);
    tasks.forEach(
        task -> {
          try {
            task.join();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        });
    tasks.stream().map(FightQueryTask::get).forEach(result::addAll);

    return result;
  }

  private static FightQueryTask createSearchTask(String c, String original, String dest) {
    return new FightQueryTask(c,original,dest);
  }
}
